﻿using System;
using System.Data;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Maticsoft.BLL;
using Maticsoft.Model;

namespace FineUI.Examples
{
    public partial class teacher_submit : System.Web.UI.Page
    { 
        /// <summary>
        /// 获取教师名
        /// </summary>
        private void SetTeacherName()
        {
            if (Session["UserName"]== null)
            {
                Response.Redirect("~/login.aspx");
                return;
            }
            string CurrentTeaUserName = Session["UserName"].ToString();
            Maticsoft.BLL.Teacher CurrentTeacher = new Maticsoft.BLL.Teacher();
            Maticsoft.Model.Teacher CurrentTeaInfom = CurrentTeacher.GetModel(CurrentTeaUserName);
            labTeacherName.Text = CurrentTeaInfom.T_Name.ToString();
        }


        protected void ExistsPermiss(string P_Id, string P_Status)
        {
            Maticsoft.BLL.Permissions PM = new Maticsoft.BLL.Permissions();
            if (!PM.ExistsPermiss(P_Id, P_Status))
            {
                Response.Redirect("~/user_no_permission.aspx");
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("~/login.aspx");
                return;
            }
            ExistsPermiss("T001", "已授权");

            if (!IsPostBack)
            {
                SetTeacherName();
                LoadTeachingCourseInform();

            }
        
        }

        protected DataSet LoadTeachingCourseInform()
        {
            


          Maticsoft.BLL.ReportTemplate DocTemplate = new Maticsoft.BLL.ReportTemplate();
            string currentT_Id=Session["UserName"].ToString();

            DataSet DSTemplate = DocTemplate.GetTeachingClassCourseName("TeachingClass.T_Id='" + currentT_Id + "' order by TC_Name" );
          //  DataSet DSTemplate = DocTemplate.GetList("TeachingClass.TC_Id=ReportTemplate.TC_Id and TeachingClass.T_Id='" + currentT_Id + "'");

            DataView CurrentView = DSTemplate.Tables[0].DefaultView;
            DropDownListCourse.DataTextField = "TC_Name";
            DropDownListCourse.DataValueField = "TC_Id";
            DropDownListCourse.DataSource = CurrentView;
            DropDownListCourse.DataBind();

            return DSTemplate;
   
        }
        

        protected void btnCreateDoc_Click(object sender, EventArgs e)
        {
            string s = datepickerStart.Text;
            //if (Convert.ToDateTime(datepickerStart.Text) > DateTime.Now) 
            //{
            //    Alert.Show("开始时间不能大于当前时间", MessageBoxIcon.Information);
            //    return;
            //}
            if (Convert.ToDateTime(datepickerStart.Text) > Convert.ToDateTime(datepickeEnd.Text))
            {
                Alert.Show("开始时间不能大于结束时间", MessageBoxIcon.Information);
                return;
            }
            if (Convert.ToDateTime(datepickeEnd.Text) < DateTime.Now)
            {
                Alert.Show("开始时间不能大于当前时间", MessageBoxIcon.Information);
                return;
            }

 
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^.+\.(doc)|(docx)$");
            if (!regex.IsMatch(fileUploadDoc.FileName))
            {
                Alert.Show("不支持该文件格式", MessageBoxIcon.Information);
                return;

            }
       
                Maticsoft.BLL.ReportTemplate documentation = new Maticsoft.BLL.ReportTemplate();
                Maticsoft.Model.ReportTemplate documentInfom = new Maticsoft.Model.ReportTemplate();
                TimeSpan document_time = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                fileUploadDoc.SaveAs(Server.MapPath(@"~/document/teacher/") + "\\" + Convert.ToInt64(document_time.TotalSeconds).ToString() + fileUploadDoc.ShortFileName.ToString().Replace(" ", ""));



                Guid guidDoc = Guid.NewGuid();



                documentInfom.R_Id = guidDoc.ToString();




                documentInfom.R_Title = DropDownListCourse.SelectedText.ToString();
                documentInfom.TC_Id = DropDownListCourse.SelectedValue.ToString();
               
               // documentInfom.T_Id = Session["UserName"].ToString(); 教学班 
               // documentInfom.R_Title = tbxCourseTitle.Text.ToString();
                documentInfom.R_Task = txareaTask.Text.ToString();
                documentInfom.R_STime = datepickerStart.Text.ToString();
                documentInfom.R_ETime = datepickeEnd.Text.ToString();
                documentInfom.R_Status = "未发布";

                documentInfom.R_Formerly = fileUploadDoc.ShortFileName.ToString().Replace(" ", "");



                documentInfom.R_Path = Convert.ToInt64(document_time.TotalSeconds).ToString() + fileUploadDoc.ShortFileName.ToString().Replace(" ", "");

                if (documentation.Add(documentInfom))
                {
                    Alert.Show("报告创建成功", MessageBoxIcon.Information);
                }
            


        }


      
    }
}